<?php
namespace app\business;

use think\Exception;
use think\facade\Filesystem;

class Image {

    protected $supportTypes = ["gif", "jpg", "png", "jpeg"];

    protected $maxSize = 1048576; // 支持最大的大小1M,默认为B

    public function save(\think\File $file, $path) : string {
        $result = validate(['file' => ['fileSize' => $this->maxSize, 'fileExt'=> $this->supportTypes]])
            ->check(['file' => $file]);
        if (!$result) {
            throw new Exception("图片格式不正确");
        }

        $filePath = Filesystem::disk('public')->putFile($path,$file);

        if ($filePath) {
            return config("filesystem.disks.public.url") . DIRECTORY_SEPARATOR . $filePath;
        }

        throw new Exception("上传失败");
    }

}